//
//  YHIMUIKitConvCellNode.m
//  YHChat
//
//  Created by apple on 2019/4/4.
//  Copyright © 2019 yinhe. All rights reserved.
//

#import "YHIMUIKitConvCellNode.h"

#import "YHIMUIKitConvCellData.h"
#import "YHIMUIKitHelper.h"

#define kHeadHeight     45.0
#define kUnreadHeight   25.0

@interface YHIMUIKitConvCellNode()
@property (nonatomic, strong) ASNetworkImageNode *headImageNode;
@property (nonatomic, strong) ASTextNode *titleNode;
@property (nonatomic, strong) ASTextNode *subTitleNode;
@property (nonatomic, strong) ASTextNode *timeNode;
@property (nonatomic, strong) ASDisplayNode *unReadBackgroundNode;
@property (nonatomic, strong) ASTextNode *unReadNode;
@end

@implementation YHIMUIKitConvCellNode

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.headImageNode = [[ASNetworkImageNode alloc] init];
        self.headImageNode.style.preferredSize = CGSizeMake(kHeadHeight, kHeadHeight);
        self.headImageNode.cornerRadius = kHeadHeight / 2.0;
        self.headImageNode.clipsToBounds = YES;
        [self addSubnode:self.headImageNode];
        
        self.titleNode = [[ASTextNode alloc] init];
        self.titleNode.maximumNumberOfLines = 1;
        self.titleNode.style.flexShrink = 1;
        [self addSubnode:self.titleNode];
        
        self.subTitleNode = [[ASTextNode alloc] init];
        self.subTitleNode.maximumNumberOfLines = 1;
        self.subTitleNode.style.flexShrink = 1;
        [self addSubnode:self.subTitleNode];
        
        self.timeNode = [[ASTextNode alloc] init];
        self.timeNode.maximumNumberOfLines = 1;
        self.timeNode.style.flexShrink = 1;
        [self addSubnode:self.timeNode];
        
        
        self.unReadBackgroundNode = [[ASDisplayNode alloc] init];
        self.unReadBackgroundNode.cornerRadius = kUnreadHeight / 2.0;
        self.unReadBackgroundNode.clipsToBounds = YES;
        [self addSubnode:self.unReadBackgroundNode];
        
        
        self.unReadNode = [[ASTextNode alloc] init];
        self.unReadNode.maximumNumberOfLines = 1;
        self.unReadNode.style.flexShrink = 1;
        [self.unReadBackgroundNode addSubnode:self.unReadNode];
        
        
        __weak typeof(self) weakSelf = self;
        self.unReadBackgroundNode.layoutSpecBlock = ^ASLayoutSpec * _Nonnull(__kindof ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
            return [ASCenterLayoutSpec centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringXY sizingOptions:ASCenterLayoutSpecSizingOptionDefault child:weakSelf.unReadNode];
        };
        
        self.headImageNode.backgroundColor = [UIColor orangeColor];
        //self.titleNode.backgroundColor = [UIColor magentaColor];
        //self.subTitleNode.backgroundColor = [UIColor purpleColor];
        //self.timeNode.backgroundColor = [UIColor greenColor];
        self.unReadBackgroundNode.backgroundColor = YHIMUIKit_RGB(255, 70, 94);
    }
    return self;
}

- (void)didLoad{
    [super didLoad];
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize{
    
    ASStackLayoutSpec *stackLayoutSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[self.titleNode, self.subTitleNode]];
    stackLayoutSpec.style.flexShrink = 1;
    stackLayoutSpec.style.flexGrow = 0;
    
    
    
    
    ASStackLayoutSpec *infoStackLayoutSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsEnd children:@[self.timeNode, self.unReadBackgroundNode]];
    
    
    ASStackLayoutSpec *contentStackLayoutSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:12 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[self.headImageNode, stackLayoutSpec]];
    contentStackLayoutSpec.style.flexShrink = 1;
    contentStackLayoutSpec.style.flexGrow = 0;
    
    ASStackLayoutSpec *finalStackLayoutSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:10 justifyContent:ASStackLayoutJustifyContentSpaceBetween alignItems:ASStackLayoutAlignItemsCenter children:@[contentStackLayoutSpec, infoStackLayoutSpec]];
    
    return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(16, 15, 16, 15) child:finalStackLayoutSpec];
}

#pragma mark ------------------ Setter ------------------
- (void)setCellData:(YHIMUIKitConvCellData *)cellData{
    _cellData = cellData;
    
    self.titleNode.attributedText = [YHIMUIKitHelper attributedStringWithText:_cellData.title color:YHIMUIKit_RGB(51, 51, 51) font:[UIFont boldSystemFontOfSize:15] kern:0 lineSpacing:0 alignment:NSTextAlignmentLeft];
    self.subTitleNode.attributedText = [YHIMUIKitHelper attributedStringWithText:_cellData.subTitle color:YHIMUIKit_RGB(102, 102, 102) font:[UIFont systemFontOfSize:12] kern:0 lineSpacing:0 alignment:NSTextAlignmentLeft];
    self.timeNode.attributedText = [YHIMUIKitHelper attributedStringWithText:[YHIMUIKitHelper transformMessageDate:_cellData.messageDate] color:YHIMUIKit_RGB(153, 153, 153) font:[UIFont systemFontOfSize:12] kern:0 lineSpacing:0 alignment:NSTextAlignmentLeft];
    
    self.unReadBackgroundNode.hidden = _cellData.unReadCount == 0 ? YES : NO;
    
    NSString *unRead = [NSString stringWithFormat:@"%d", _cellData.unReadCount];
    NSMutableAttributedString *unReadAtr = [YHIMUIKitHelper attributedStringWithText:unRead color:YHIMUIKit_RGB(255, 255, 255) font:[UIFont boldSystemFontOfSize:13] kern:0 lineSpacing:0 alignment:NSTextAlignmentLeft];
    self.unReadNode.attributedText = unReadAtr;
    
    
    ASLayout *unReadLayout = [self.unReadNode layoutThatFits:ASSizeRangeMake(CGSizeMake(0, 0), CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX))];
    CGFloat unReadWidth = unReadLayout.size.width;
    
    if (unReadWidth + 5.0 * 2 <= kUnreadHeight) {
        self.unReadBackgroundNode.style.preferredSize = CGSizeMake(kUnreadHeight, kUnreadHeight);
    } else {
        self.unReadBackgroundNode.style.preferredSize = CGSizeMake(unReadWidth + 5.0 * 2, kUnreadHeight);
    }
}

@end
